import 'package:api/pages/network/Dio/post/post_model.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

void main() => runApp(RootApp());

// 定义根目录Wigdget
class RootApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 根视图
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String result = "";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Dio网络请求 GET")),
      body: Row(
        children: [
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                FlatButton(
                  onPressed: () {
                    // TODO
                    fileUpload();
                  },
                  child: Text("发起网络请求"),
                ),
                Text("文章ID ${post != null ? post.id : ''}"),
                Text("文章标题 ${post != null ? post.title : ''}"),
                Text("文章详情 ${post != null ? post.body : ''}"),
              ],
            ),
          ),
        ],
      ),
    );
  }

  PostModel post;
  void fileUpload() async {
    Dio dio = new Dio();
    // 请求地址
    String url = "https://jsonplaceholder.typicode.com/posts";
    String localPath = "";
    // 创建参数
    Map<String, dynamic> map = new Map();
    map['auth'] = "demo";
    map['file'] = await MultipartFile.fromFile(localPath, filename: "ceshi");
    FormData formData = FormData.fromMap(map);
    // 发起post请求

    Response response = await dio.post(url, data: formData,
        onSendProgress: (int progress, int total) {
      print("上传进度 $progress  总 $total");
    });
    // 相应数据
    var data = response.data;
  }
}
